package com.knight.study.patterns.tujie.command;

import java.util.Iterator;
import java.util.Stack;

/**
 * @Author knight
 * @Date 2024/9/12 17:26
 */

public class MacroCommand implements Commend{

    private Stack commands = new Stack();

    @Override
    public void execute() {
        Iterator it = commands.iterator();
        while (it.hasNext()) {
            ((Commend)it.next()).execute();
        }
    }

    public void append(Commend cmd) {
        if (cmd != this) {
            commands.push(cmd);
        }
    }

    //删除最后一条
    public void undo() {
        if (!commands.empty()) {
            commands.pop();
        }
    }

    public void clear() {
        commands.clear();
    }
}
